package com.cskaoyan;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;

/**
 * 创建日期: 2022/08/31 11:10
 *
 * @author ciggar
 */
public class Main {


    public static void main(String[] args) throws IOException {

//        读取核心配置文件
//        String resource = "org/mybatis/example/mybatis-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//
//        获取SqlSessionFactory的实例对象
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

//        ClassLoader classLoader = Main.class.getClassLoader();
////        InputStream inputStream = classLoader.getResourceAsStream("mybatis-config.xml");


        // 1. 获取核心配置文件的文件流
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");


        // 2. 获取SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

        // 3. 通过SqlSessionFactory 获取SqlSession
        // sqlSession其实就是Mybatis和 MySQL数据库之间的连接，就是之前的Connection对象的封装
        // 获取的SqlSession是不会自动提交事务的，如果需要自动提交事务，openSession(true);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        // 4. 执行SQL语句
        int affectedRows = sqlSession.delete("cskaoyan.deleteUserById", 1004);

        System.out.println("affectedRows:" + affectedRows);


    }
}
